//
//  PersonCenterViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/1/27.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "PersonCenterViewController.h"
#import "PersionInfoViewController.h"
#import "UIButton+WebCache.h"
#import "VIPWebViewController.h"
@interface PersonCenterViewController ()
@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UILabel *noticeNumberLabel;//通知消息个数
@property (strong, nonatomic) IBOutlet UIButton *headImage;//头像
@property (strong, nonatomic) IBOutlet UILabel *phoneId;//昵称
@property (strong, nonatomic) IBOutlet UILabel *codeLabel;//积分
@property (strong, nonatomic) IBOutlet UILabel *walletLabel;//钱包
@property (strong, nonatomic) IBOutlet UILabel *CouponLabel;//优惠劵
@property (strong, nonatomic) IBOutlet UILabel *waitPayNumber;//待付款个数
@property (strong, nonatomic) IBOutlet UILabel *waitPostNumber;//代发货个数
@property (strong, nonatomic) IBOutlet UILabel *waitUseNumber;//待使用货个数
@property (strong, nonatomic) IBOutlet UILabel *commentNumber;//待评价个数
@property (strong, nonatomic) IBOutlet UILabel *returnNumber;//退货个数

@property (strong, nonatomic) IBOutlet UIImageView *personLevel;//用户会员等级
@property (strong, nonatomic) IBOutlet UILabel *personType;//用户类型
@property (strong, nonatomic) IBOutlet UILabel *ifAttrest;//是否认证
@property (strong, nonatomic) IBOutlet UILabel *servicePhone;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@property (strong, nonatomic) NSString *servicePhoneStr;

@property(nonatomic,assign)NSInteger indentState;
@end

@implementation PersonCenterViewController


- (void)viewWillDisappear:(BOOL)animated{
    [[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"ifLogin"];
}

- (void)viewWillAppear:(BOOL)animated{
    if ([Common getLoginStatus]) {
//        _waitPayNumber.hidden = NO;
//        _waitPostNumber.hidden = NO;
//        _waitUseNumber.hidden = NO;
//        _returnNumber.hidden = NO;
//        _commentNumber.hidden = NO;
        
        NSDictionary * dic =  [Common getLoginInfo];
        _phoneId.text = dic[@"userPhone"];
        _phoneId.hidden = NO;
        _personType.hidden = NO;
        _ifAttrest.hidden = NO;
        NSInteger userType = [dic[Key_User_suseType] integerValue];
        _ifAttrest.hidden = YES;
        if (userType%16==0) {
            _personType.text = @"普通用户";
            _ifAttrest.hidden = NO;
        }else if(userType%16%4%8==0){
            _personType.text = @"商铺用户";
        }else if(userType%16%4==0){
            _personType.text = @"业主用户";
        }else if(userType%16%8==0){
            _personType.text = @"商铺用户";
        }else if(userType%8==0){
            _personType.text = @"商铺用户";
        }else if(userType%8%4==0){
            _personType.text = @"商铺用户";
        }else if(userType%4==0){
            _personType.text = @"业主用户";
        }else if(userType%1==0){
            _personType.text = @"管理员";
        }else if(userType%1%8==0){
            _personType.text = @"管理员";
        }else if(userType%1%16==0){
            _personType.text = @"管理员";
        }else if(userType%1%4==0){
            _personType.text = @"管理员";
        }
        
        [self getNoticeNumber];
        [self getPersonInfo];
        [self querySysWebsiteConfig];
        _indentState = 1;
        [self getIndentNumber];
        //积分
        [self getPersonalCode];
        //代金卷优惠劵数量
        [self queryAppVoucherCount];
        
      _personType.text = @"商铺用户";    //测试商家用户
        
        if ([_personType.text isEqualToString:@"商铺用户"]) {
            _bottomView.frame = flexibleFrame(CGRectMake(0, 358, 320, 280), NO);
            _scroView.contentSize = flexibleSize(CGSizeMake(0, 645), NO);
        }else{
            _bottomView.frame = flexibleFrame(CGRectMake(0, 313, 320, 280), NO);
            _scroView.contentSize = flexibleSize(CGSizeMake(0, 600), NO);
        }
        
    }else{
        //订单状态
        _waitPayNumber.hidden = YES;
        _waitPostNumber.hidden = YES;
        _waitUseNumber.hidden = YES;
        _returnNumber.hidden = YES;
        _commentNumber.hidden = YES;
        
        _phoneId.hidden = YES;
        _personType.hidden = YES;
        _ifAttrest.hidden = YES;
//        //弹出登陆
//        [self ShowLoginPage];
//        return;
        _personType.hidden = YES;
        _ifAttrest.hidden = YES;
        _bottomView.frame = flexibleFrame(CGRectMake(0, 313, 320, 280), NO);
        _scroView.contentSize = flexibleSize(CGSizeMake(0, 600), NO);
        //弹出登陆
//        [self ShowLoginPage];
        return;
    }
    
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"ifLogin"]isEqualToString:@"1"]) {
         self.tabBarController.selectedIndex = 0;
        
    }
   
    //账户余额
    [self getPersonalLeftMoney];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.servicePhoneStr = @"";
    [self initViewLayout];
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.scroView];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.bottomView];
    _scroView.frame = CGRectMake(0, 0,320*VerticalRatio(), 568*VerticalRatio()-49);
    _scroView.contentSize = CGSizeMake(0, 568*VerticalRatio()-48);
    _scroView.showsVerticalScrollIndicator= NO;
    
   
    self.noticeNumberLabel.layer.masksToBounds = YES;
    self.noticeNumberLabel.layer.cornerRadius = 6*VerticalRatio();
    _waitPayNumber.layer.masksToBounds = YES;
    _waitPayNumber.layer.cornerRadius = 6*VerticalRatio();
    
    _waitPostNumber.layer.masksToBounds = YES;
    _waitPostNumber.layer.cornerRadius = 6*VerticalRatio();
    
    _waitUseNumber.layer.masksToBounds = YES;
    _waitUseNumber.layer.cornerRadius = 6*VerticalRatio();
    
    _returnNumber.layer.masksToBounds = YES;
    _returnNumber.layer.cornerRadius = 6*VerticalRatio();
    
    self.commentNumber.layer.masksToBounds = YES;
    self.commentNumber.layer.cornerRadius = 6*VerticalRatio();
    
    self.headImage.layer.masksToBounds = YES;
    self.headImage.layer.cornerRadius = 40*VerticalRatio();
    
    
    _personType.layer.masksToBounds = YES;
    _ifAttrest.layer.masksToBounds = YES;
    _personType.layer.cornerRadius = 3*VerticalRatio();
    _ifAttrest.layer.cornerRadius = 3*VerticalRatio();
  
}

#pragma mark == 设置
- (IBAction)setting:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Setting"];
    [self.navigationController   pushViewController:page animated:YES];
}
#pragma mark == 通知中心
- (IBAction)noticeCenter:(id)sender {
    if (![Common getLoginStatus]) {
        [self ShowLoginPage];
        return;
    }
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"NoticeCenter"];
    [self.navigationController   pushViewController:page animated:YES];
    
}

#pragma mark == 请求个人钱包余额
- (void)getPersonalLeftMoney{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_getEpurse withHttpType:@"POST" withTag:SX_SX_getEpurse];
}

#pragma mark == 请求个人总积分
- (void)getPersonalCode{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryMypoits withHttpType:@"POST" withTag:SX_SX_queryMypoits];
}
#pragma mark == 请求代金券优惠券数量
- (void)queryAppVoucherCount{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryAppVoucherCount withHttpType:@"POST" withTag:SX_queryAppVoucherCountTag];
    
    
}
#pragma mark === 获取消息条数
- (void)getNoticeNumber{
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryfincationCount withHttpType:@"POST" withTag:SX_SX_queryfincationCount];

}

#pragma mark === 获取各状态订单个数
- (void)getIndentNumber{
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    if (_indentState==1) {//带支付
         [dic setObject:@"1" forKey:@"stateId"];
    }else if (_indentState==2){//代发货
        [dic setObject:@"2" forKey:@"stateId"];
    }else if (_indentState==3){//待收货
         [dic setObject:@"4" forKey:@"stateId"];
    }else if (_indentState==4){//待评价
         [dic setObject:@"5" forKey:@"stateId"];
    }else if (_indentState==5){//退货
         [dic setObject:@"7" forKey:@"stateId"];
    }
   
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_countOrderState withHttpType:@"POST" withTag:SX_SX_countOrderState];
    
}

#pragma mark == 客服热线
- (IBAction)callService:(id)sender {
    [Common callPhone:self.servicePhoneStr view:self.view];
}
#pragma mark == 个人资料
- (IBAction)changePersonInfo:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [self ShowLoginPage];
        return;
    }
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
    PersionInfoViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"PersionInfo"];
    [self.navigationController   pushViewController:page animated:YES];
}
#pragma mark == 积分
- (IBAction)myCode:(id)sender {
    if(![Common getLoginStatus]){
        [self ShowLoginPage];
        return;
    }else{
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"MyCodeStoryboard" bundle:nil];
        UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"PersonCode"];
        [self.navigationController   pushViewController:page animated:YES];
    }
}
#pragma mark == 钱包
- (IBAction)walletHandel:(id)sender {
    if(![Common getLoginStatus]){
        [self ShowLoginPage];
        return;
    }
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Wallet"];
    [self.navigationController   pushViewController:page animated:YES];
}
#pragma mark == 优惠劵
- (IBAction)couponHandel:(id)sender {
    if(![Common getLoginStatus]){
        [self ShowLoginPage];
        return;
    }else{
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
        UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"MyCards"];
        [self.navigationController   pushViewController:page animated:YES];
    }
    
    
}
#pragma mark == 所以订单
- (IBAction)allIndent:(id)sender {
    if(![Common getLoginStatus]){
        [self ShowLoginPage];
        return;
    }else{
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
        UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"AllIndent"];
        [self.navigationController   pushViewController:page animated:YES];
    }
}
#pragma mark == 代付款之后的按钮点击事件
- (IBAction)actionHandel:(UIButton *)sender {
    if(![Common getLoginStatus]){
        [self ShowLoginPage];
        return;
    }
    switch (sender.tag) {
        case 100:   //代付款
        {
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
            UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"WaitingPay"];
            [self.navigationController   pushViewController:page animated:YES];
        }
            break;
        case 101://代收货
        {
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
            UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"WaitingRecive"];
            [self.navigationController   pushViewController:page animated:YES];
        }
            break;
        case 102://代评价
        {
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
            UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"WaitingComment"];
            [self.navigationController   pushViewController:page animated:YES];
        }
            break;
        case 103://退货
        {
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
            UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ReturnGoods"];
            [self.navigationController   pushViewController:page animated:YES];
            
        }
            break;
        case 104://我的店铺
        {
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
            UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"MyStore"];
            [self.navigationController   pushViewController:page animated:YES];
        }
            break;
        case 105://停车缴费
        {
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
            UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"MyParkCars"];
            [self.navigationController   pushViewController:page animated:YES];
        }
            break;
        case 106://我的活动
        {
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
            UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"MyActivity"];
            [self.navigationController   pushViewController:page animated:YES];
        }
            break;
        case 107://我的收藏
        {
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
            UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"MyCollection"];
            [self.navigationController   pushViewController:page animated:YES];
        }
            break;
        case 108://我的足迹
        {
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
            UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"MyHistory"];
            [self.navigationController   pushViewController:page animated:YES];
        }
            break;
        case 109://推荐有奖
        {
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"VIPServiceStoryboard" bundle:nil];
            VIPWebViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"VIPWeb"];
            page.url = SX_memberRecommendPrize;
            page.titleStr = @"推荐有奖";
            [self.navigationController  pushViewController:page animated:YES];

        
        }
            break;
        case 110://待发货
        {
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonCenterStoryboard" bundle:nil];
            UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"WaitShipments"];
            [self.navigationController   pushViewController:page animated:YES];
            
        }
            break;
        
        default:
            break;
    }
}


#pragma mark == 获取用户信息
- (void)getPersonInfo{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryPersonalData withHttpType:@"POST" withTag:SX_SX_queryPersonalData];
}
#pragma mark == 获取客服电话
- (void)querySysWebsiteConfig{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_querySysWebsiteConfig withHttpType:@"POST" withTag:SX_querySysWebsiteConfigTag];
}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag == SX_SX_queryfincationCount){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSInteger num = [database[@"result"][@"count"]integerValue];
            if (num == 0) {
                _noticeNumberLabel.hidden = YES;
            }else{
                _noticeNumberLabel.hidden = NO;
                _noticeNumberLabel.text = [NSString stringWithFormat:@"%@",database[@"result"][@"count"]];
            }
        }
    }else if (tag==SX_SX_queryPersonalData) {
        
        if ([[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSArray * array = database[@"result"];
            if (array.count==0) {
                return;
            }
            NSDictionary * dic = database[@"result"][0];
            NSString * head = [NSString stringWithFormat:@"%@",dic[@"resImagePath"]];
            if (head.length>0) {
                [_headImage sd_setBackgroundImageWithURL:[NSURL URLWithString:head] forState:UIControlStateNormal];
            }
            _headImage.layer.masksToBounds = YES;
            _headImage.layer.cornerRadius = 40*VerticalRatio();

            NSString * vipLevel = [NSString stringWithFormat:@"%@",dic[@"memberVipImg"]];
            if (vipLevel.length>0) {
                [_personLevel sd_setImageWithURL:[NSURL URLWithString:vipLevel]];
            }
            
        }
    }else if (tag == SX_querySysWebsiteConfigTag){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            self.servicePhoneStr = database[@"result"];
            self.servicePhone.text = [NSString stringWithFormat:@"客服热线: %@",self.servicePhoneStr];
            
        }
    }else if (tag == SX_SX_countOrderState){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            if (_indentState==1) {
                NSInteger num = [database[@"result"][@"countOrder"]integerValue];
                if (num == 0) {
                    _waitPayNumber.hidden = YES;
                }else{
                    _waitPayNumber.hidden = NO;
                    _waitPayNumber.text = [NSString stringWithFormat:@"%@",database[@"result"][@"countOrder"]];
                }
               
            }else if (_indentState==2){
                NSInteger num = [database[@"result"][@"countOrder"]integerValue];
                if (num == 0) {
                    _waitPostNumber.hidden = YES;
                }else{
                    _waitPostNumber.hidden = NO;
                    _waitPostNumber.text = [NSString stringWithFormat:@"%@",database[@"result"][@"countOrder"]];
                }
            }else if (_indentState==3){
                NSInteger num = [database[@"result"][@"countOrder"]integerValue];
                if (num == 0) {
                    _waitUseNumber.hidden = YES;
                }else{
                    _waitUseNumber.hidden = NO;
                    _waitUseNumber.text = [NSString stringWithFormat:@"%@",database[@"result"][@"countOrder"]];
                }

            }else if (_indentState==4){
                NSInteger num = [database[@"result"][@"countOrder"]integerValue];
                if (num == 0) {
                    _commentNumber.hidden = YES;
                }else{
                    _commentNumber.hidden = NO;
                    _commentNumber.text = [NSString stringWithFormat:@"%@",database[@"result"][@"countOrder"]];
                }
            }else if (_indentState==5){
                NSInteger num = [database[@"result"][@"countOrder"]integerValue];
                if (num == 0) {
                    _returnNumber.hidden = YES;
                }else{
                    _returnNumber.hidden = NO;
                    _returnNumber.text = [NSString stringWithFormat:@"%@",database[@"result"][@"countOrder"]];
                }
            }
            _indentState +=1;
            if (_indentState>5) {
                return;
            }
            [self getIndentNumber];
        }
    
    }else if (tag == SX_SX_queryMypoits){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            _codeLabel.text = [NSString stringWithFormat:@"%@",database[@"result"][@"myPoints"]];
        }
    }else if (tag == SX_queryAppVoucherCountTag){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            _CouponLabel.text = [NSString stringWithFormat:@"%@",database[@"result"][@"vcCount"]];
        }
    }else if (tag == SX_SX_getEpurse){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            _walletLabel.text = [NSString stringWithFormat:@"￥%.2f",[database[@"result"][@"epurse"]floatValue]];
        }
    
    }
}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
